home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / OpenURL / Developer / Source / library_prefs.c < prev    next >
C/C++ Source or Header  |  1999-09-26  |  12KB  |  504 lines

  1. /* 
  2. ** openurl.library - universal URL display and browser launcher library
  3. ** Written by Troels Walsted Hansen <troels@thule.no>
  4. ** Placed in the public domain.
  5. **
  6. ** Module with all prefs related library support functions.
  7. */
  8.  
  9. #include "library_common.h"
  10. #include "library_api.h"
  11. #include "library_prefs.h"
  12. #include "library_util.h"
  13.  
  14. /**************************************************************************
  15. *
  16. * Definitions.
  17. *
  18. */
  19.  
  20. #define ID_BRWS MAKE_ID('B','R','W','S')
  21. #define ID_MLRS MAKE_ID('M','L','R','S')
  22. #define ID_FLGS MAKE_ID('F','L','G','S')
  23. #define ID_DEFS MAKE_ID('D','E','F','S')
  24.  
  25. #define BRWS_SIZE (sizeof(struct URL_BrowserNode) - sizeof(struct MinNode))
  26. #define MLRS_SIZE (sizeof(struct URL_MailerNode) - sizeof(struct MinNode))
  27. #define FLGS_SIZE (sizeof(LONGBITS))
  28. #define DEFS_SIZE (4 * sizeof(BOOL))
  29.  
  30. /**************************************************************************
  31. *
  32. * Externally visible functions.
  33. *
  34. */
  35.  
  36. struct URL_Prefs *CopyPrefs(struct URL_Prefs *old_p)
  37. {
  38.     struct URL_Prefs *new_p;
  39.  
  40.     /* make a copy of a prefs structure */
  41.  
  42.     if(!(new_p = AllocMem(sizeof(struct URL_Prefs), MEMF_ANY)))
  43.         return(NULL);
  44.  
  45.     memcpy(new_p, old_p, sizeof(struct URL_Prefs));
  46.     NewList((struct List *)&new_p->up_BrowserList);
  47.     NewList((struct List *)&new_p->up_MailerList);
  48.  
  49.     if(!CopyList((struct List *)&new_p->up_BrowserList, 
  50.                  (struct List *)&old_p->up_BrowserList,
  51.                  sizeof(struct URL_BrowserNode)))
  52.     {
  53.         LIB_URL_FreePrefs(new_p);
  54.         return(NULL);
  55.     }
  56.  
  57.     if(!CopyList((struct List *)&new_p->up_MailerList, 
  58.                  (struct List *)&old_p->up_MailerList,
  59.                  sizeof(struct URL_MailerNode)))
  60.     {
  61.         LIB_URL_FreePrefs(new_p);
  62.         return(NULL);
  63.     }
  64.  
  65.     return(new_p);
  66. }
  67.  
  68. /**************************************************************************/
  69.  
  70. BOOL SavePrefs(STRPTR filename, struct URL_Prefs *up)
  71. {
  72.     BOOL retval = FALSE;
  73.     struct IFFHandle *iffh;
  74.     struct PrefHeader prhd;
  75.     struct URL_BrowserNode *bn;
  76.     struct URL_MailerNode *mn;
  77.  
  78.     /* init iff handle */
  79.  
  80.     if(!(iffh = AllocIFF()))
  81.         goto done;
  82.  
  83.     if(!(iffh->iff_Stream = Open(filename, MODE_NEWFILE)))
  84.         goto done;
  85.  
  86.     InitIFFasDOS(iffh);
  87.  
  88.     if(OpenIFF(iffh, IFFF_WRITE))
  89.         goto done;
  90.  
  91.     /* init file as IFF PREF FORM */
  92.  
  93.     if(PushChunk(iffh, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
  94.         goto done;
  95.  
  96.     /* write pref header */
  97.  
  98.     if(PushChunk(iffh, ID_PREF, ID_PRHD, sizeof(struct PrefHeader)))
  99.         goto done;
  100.  
  101.     prhd.ph_Version = up->up_Version;
  102.     prhd.ph_Type    = 0;
  103.     prhd.ph_Flags   = 0;
  104.  
  105.     if(WriteChunkBytes(iffh, &prhd, sizeof(struct PrefHeader)) 
  106.         != sizeof(struct PrefHeader))
  107.         goto done;
  108.  
  109.     if(PopChunk(iffh))
  110.         goto done;
  111.  
  112.     /* write browser nodes */
  113.  
  114.     for(bn = (struct URL_BrowserNode *)Prefs->up_BrowserList.mlh_Head;
  115.         bn->ubn_Node.mln_Succ;
  116.         bn = (struct URL_BrowserNode *)bn->ubn_Node.mln_Succ)
  117.     {
  118.         if(PushChunk(iffh, ID_PREF, ID_BRWS, BRWS_SIZE))
  119.             goto done;
  120.  
  121.         if(WriteChunkBytes(iffh, &bn->ubn_Flags, BRWS_SIZE) != BRWS_SIZE)
  122.             goto done;
  123.  
  124.         if(PopChunk(iffh))
  125.             goto done;
  126.     }
  127.  
  128.     /* write mailer nodes */
  129.  
  130.     for(mn = (struct URL_MailerNode *)Prefs->up_MailerList.mlh_Head;
  131.         mn->umn_Node.mln_Succ;
  132.         mn = (struct URL_MailerNode *)mn->umn_Node.mln_Succ)
  133.     {
  134.         if(PushChunk(iffh, ID_PREF, ID_MLRS, MLRS_SIZE))
  135.             goto done;
  136.  
  137.         if(WriteChunkBytes(iffh, &mn->umn_Flags, MLRS_SIZE) != MLRS_SIZE)
  138.             goto done;
  139.  
  140.         if(PopChunk(iffh))
  141.             goto done;
  142.     }
  143.  
  144.     /* write flags */
  145.  
  146.     if(PushChunk(iffh, ID_PREF, ID_FLGS, FLGS_SIZE))
  147.         goto done;
  148.  
  149.     if(WriteChunkBytes(iffh, &Prefs->up_Flags, FLGS_SIZE) != FLGS_SIZE)
  150.         goto done;
  151.  
  152.     if(PopChunk(iffh))
  153.         goto done;
  154.  
  155.     /* write defaults */
  156.  
  157.     if(PushChunk(iffh, ID_PREF, ID_DEFS, DEFS_SIZE))
  158.         goto done;
  159.  
  160.     if(WriteChunkBytes(iffh, &Prefs->up_DefShow, DEFS_SIZE) != DEFS_SIZE)
  161.         goto done;
  162.  
  163.     if(PopChunk(iffh))
  164.         goto done;
  165.  
  166.     /* pop the IFF PREF FORM chunk */
  167.  
  168.     if(PopChunk(iffh))
  169.         goto done;
  170.  
  171.     retval = TRUE;
  172. done:
  173.     if(iffh)
  174.     {
  175.         CloseIFF(iffh);
  176.         Close(iffh->iff_Stream);
  177.         FreeIFF(iffh);
  178.     }
  179.  
  180.     if(!retval) DeleteFile(filename);    
  181.  
  182.     return(retval);
  183. }
  184.  
  185. /**************************************************************************/
  186.  
  187. BOOL LoadPrefs(STRPTR filename)
  188. {
  189.     BOOL retval = FALSE;
  190.     struct IFFHandle *iffh = NULL;
  191.     struct ContextNode *cn;
  192.     struct PrefHeader prhd;
  193.  
  194.     ObtainSemaphore(&PrefsSemaphore);
  195.  
  196.     /* allocate main prefs structure */
  197.  
  198.     if(!(Prefs = AllocMem(sizeof(struct URL_Prefs), MEMF_CLEAR)))
  199.         goto done;
  200.  
  201.     Prefs->up_Version = PREFS_VERSION;
  202.     NewList((struct List *)&Prefs->up_BrowserList);
  203.     NewList((struct List *)&Prefs->up_MailerList);
  204.  
  205.     /* init iff handle */
  206.  
  207.     if(!(iffh = AllocIFF()))
  208.         goto done;
  209.  
  210.     if(!(iffh->iff_Stream = Open(filename, MODE_OLDFILE)))
  211.         goto done;
  212.  
  213.     InitIFFasDOS(iffh);
  214.  
  215.     if(OpenIFF(iffh, IFFF_READ))
  216.         goto done;
  217.  
  218.     /* stop at these chunks */
  219.  
  220.     if(StopChunk(iffh, ID_PREF, ID_PRHD))
  221.         goto done;
  222.  
  223.     if(StopChunk(iffh, ID_PREF, ID_DEFS))
  224.         goto done;
  225.  
  226.     if(StopChunk(iffh, ID_PREF, ID_FLGS))
  227.         goto done;
  228.  
  229.     if(StopChunk(iffh, ID_PREF, ID_MLRS))
  230.         goto done;
  231.  
  232.     if(StopChunk(iffh, ID_PREF, ID_BRWS))
  233.         goto done;
  234.  
  235.     /* check that we got a prefheader of the right version */
  236.  
  237.     if(ParseIFF(iffh, IFFPARSE_SCAN))
  238.         goto done;
  239.  
  240.     if(!(cn = CurrentChunk(iffh)))
  241.         goto done;
  242.  
  243.     if((cn->cn_Type != ID_PREF) || (cn->cn_ID != ID_PRHD) || 
  244.        (cn->cn_Size != sizeof(struct PrefHeader)))
  245.         goto done;
  246.  
  247.     if(ReadChunkBytes(iffh, &prhd, cn->cn_Size) != cn->cn_Size)
  248.         goto done;
  249.  
  250.     if(prhd.ph_Version > PREFS_VERSION)
  251.         goto done;
  252.  
  253.     if(prhd.ph_Version < 2)
  254.         SetDefaultPrefsV2(Prefs);
  255.  
  256.     if(prhd.ph_Version < 3)
  257.         SetDefaultPrefsV3(Prefs);
  258.  
  259.     /* read browser nodes */
  260.  
  261.     while(1)
  262.     {
  263.         ULONG error;
  264.  
  265.         error = ParseIFF(iffh, IFFPARSE_SCAN);
  266.         if(error == IFFERR_EOF) break;
  267.         else if(error) goto done;
  268.  
  269.         if(!(cn = CurrentChunk(iffh)))
  270.             goto done;
  271.  
  272.         if(cn->cn_Type != ID_PREF)
  273.             continue;
  274.  
  275.         if((cn->cn_ID == ID_BRWS) && (cn->cn_Size == BRWS_SIZE))
  276.         {
  277.             struct URL_BrowserNode *bn;
  278.  
  279.             if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  280.                 goto done;
  281.  
  282.             if(ReadChunkBytes(iffh, &bn->ubn_Flags, cn->cn_Size) != cn->cn_Size)
  283.             {
  284.                 FreeMem(bn, sizeof(struct URL_BrowserNode));
  285.                 goto done;
  286.             }
  287.  
  288.             AddTail((struct List *)&Prefs->up_BrowserList, (struct Node *)bn);
  289.         }
  290.         else if((cn->cn_ID == ID_MLRS) && (cn->cn_Size == MLRS_SIZE))
  291.         {
  292.             struct URL_MailerNode *mn;
  293.  
  294.             if(!(mn = AllocMem(sizeof(struct URL_MailerNode), MEMF_CLEAR)))
  295.                 goto done;
  296.  
  297.             if(ReadChunkBytes(iffh, &mn->umn_Flags, cn->cn_Size) != cn->cn_Size)
  298.             {
  299.                 FreeMem(mn, sizeof(struct URL_MailerNode));
  300.                 goto done;
  301.             }
  302.  
  303.             AddTail((struct List *)&Prefs->up_MailerList, (struct Node *)mn);
  304.         }
  305.         else if((cn->cn_ID == ID_FLGS) && (cn->cn_Size == FLGS_SIZE))
  306.         {
  307.             if(ReadChunkBytes(iffh, &Prefs->up_Flags, cn->cn_Size) != cn->cn_Size)
  308.                 goto done;
  309.         }
  310.         else if((cn->cn_ID == ID_DEFS) && (cn->cn_Size == DEFS_SIZE))
  311.         {
  312.             if(ReadChunkBytes(iffh, &Prefs->up_DefShow, cn->cn_Size) != cn->cn_Size)
  313.                 goto done;
  314.         }
  315.     }
  316.  
  317.     Prefs->up_Flags &= ~UPF_ISDEFAULTS;
  318.  
  319.     if(prhd.ph_Version == 2)
  320.         ConvertPrefsV2toV3(Prefs);
  321.  
  322.     retval = TRUE;
  323. done:
  324.     if(iffh)
  325.     {
  326.         CloseIFF(iffh);
  327.         Close(iffh->iff_Stream);
  328.         FreeIFF(iffh);
  329.     }
  330.  
  331.     if(Prefs && !retval)
  332.     {
  333.         LIB_URL_FreePrefs(Prefs);
  334.         Prefs = NULL;
  335.     }
  336.  
  337.     ReleaseSemaphore(&PrefsSemaphore);
  338.  
  339.     return(retval);
  340. }
  341.  
  342. /**************************************************************************/
  343.  
  344. BOOL SetDefaultPrefsV1(struct URL_Prefs *up)
  345. {
  346.     struct URL_BrowserNode *bn;
  347.  
  348.     NewList((struct List *)&up->up_BrowserList);
  349.  
  350.     if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  351.         return(FALSE);
  352.  
  353.     strcpy(bn->ubn_Name, "AMosaic");
  354.     strcpy(bn->ubn_Path, "AMosaic \"%u\"");
  355.     strcpy(bn->ubn_Port, "AMOSAIC");
  356.     strcpy(bn->ubn_ShowCmd, "SHOW");
  357.     strcpy(bn->ubn_ToFrontCmd, "SCREEN FRONT");
  358.     strcpy(bn->ubn_OpenURLCmd, "JUMP URL \"%u\"");
  359.  
  360.     AddTail((struct List *)&up->up_BrowserList, (struct Node *)bn);
  361.  
  362.     if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  363.         return(FALSE);
  364.  
  365.     strcpy(bn->ubn_Name, "AWeb");
  366.     strcpy(bn->ubn_Path, "AWeb-II:AWeb \"%u\"");
  367.     strcpy(bn->ubn_Port, "AWEB");
  368.     strcpy(bn->ubn_ShowCmd, "ICONIFY SHOW");
  369.     strcpy(bn->ubn_ToFrontCmd, "SCREENTOFRONT");
  370.     strcpy(bn->ubn_OpenURLCmd, "OPEN \"%u\"");
  371.     strcpy(bn->ubn_OpenURLWCmd, "NEW \"%u\"");
  372.  
  373.     AddTail((struct List *)&up->up_BrowserList, (struct Node *)bn);
  374.  
  375.     if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  376.         return(FALSE);
  377.  
  378.     strcpy(bn->ubn_Name, "IBrowse");
  379.     strcpy(bn->ubn_Path, "IBrowse \"%u\"");
  380.     strcpy(bn->ubn_Port, "IBROWSE");
  381.     strcpy(bn->ubn_ShowCmd, "SHOW");
  382.     strcpy(bn->ubn_ToFrontCmd, "SCREENTOFRONT");
  383.     strcpy(bn->ubn_OpenURLCmd, "GOTOURL \"%u\"");
  384.     strcpy(bn->ubn_OpenURLWCmd, "NEWWINDOW \"%u\"");
  385.  
  386.     AddTail((struct List *)&up->up_BrowserList, (struct Node *)bn);
  387.  
  388.     if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  389.         return(FALSE);
  390.  
  391.     strcpy(bn->ubn_Name, "moreHTML");
  392.     strcpy(bn->ubn_Port, "MOREHTML");
  393.     strcpy(bn->ubn_ShowCmd, "SHOW");
  394.     strcpy(bn->ubn_ToFrontCmd, "SCREENTOFRONT");
  395.     strcpy(bn->ubn_OpenURLCmd, "OPEN URL \"%u\"");
  396.  
  397.     AddTail((struct List *)&up->up_BrowserList, (struct Node *)bn);
  398.  
  399.     if(!(bn = AllocMem(sizeof(struct URL_BrowserNode), MEMF_CLEAR)))
  400.         return(FALSE);
  401.  
  402.     strcpy(bn->ubn_Name, "Voyager");
  403.  
  404.     if(GetVar("Vapor/Voyager_LASTUSEDDIR", bn->ubn_Path, 
  405.               UBN_PATH_LEN, GVF_GLOBAL_ONLY) != -1)
  406.         AddPart(bn->ubn_Path, "V \"%u\"", UBN_PATH_LEN);
  407.  
  408.     strcpy(bn->ubn_Port, "VOYAGER");
  409.     strcpy(bn->ubn_ShowCmd, "SHOW");
  410.     strcpy(bn->ubn_ToFrontCmd, "SCREENTOFRONT");
  411.     strcpy(bn->ubn_OpenURLCmd, "OPENURL \"%u\"");
  412.     strcpy(bn->ubn_OpenURLWCmd, "OPENURL \"%u\" NEWWIN");
  413.  
  414.     AddTail((struct List *)&up->up_BrowserList, (struct Node *)bn);
  415.  
  416.     return(TRUE);
  417. }
  418.  
  419. /**************************************************************************/
  420.  
  421. VOID SetDefaultPrefsV2(struct URL_Prefs *up)
  422. {
  423.     up->up_DefShow         = TRUE;
  424.     up->up_DefBringToFront = TRUE;
  425.     up->up_DefNewWindow    = FALSE;
  426.     up->up_DefLaunch       = TRUE;
  427. }
  428.  
  429. /**************************************************************************/
  430.  
  431. BOOL SetDefaultPrefsV3(struct URL_Prefs *up)
  432. {
  433.     struct URL_MailerNode *mn;
  434.  
  435.     up->up_Flags |= UPF_PREPENDHTTP;
  436.     up->up_Flags |= UPF_DOMAILTO;
  437.  
  438.     NewList((struct List *)&up->up_MailerList);
  439.  
  440.     if(!(mn = AllocMem(sizeof(struct URL_MailerNode), MEMF_CLEAR)))
  441.         return(FALSE);
  442.  
  443.     strcpy(mn->umn_Name, "MicroDot II");
  444.     GetVar("Vapor/MD2_LASTUSEDDIR", mn->umn_Path, UMN_PATH_LEN, GVF_GLOBAL_ONLY);
  445.     AddPart(mn->umn_Path, "MicroDot TO=\"%a\" SUBJECT=\"%s\" CONTENTS=\"%f\"", UMN_PATH_LEN);
  446.     strcpy(mn->umn_Port, "MD");
  447.     strcpy(mn->umn_ShowCmd, "SHOW");
  448.     strcpy(mn->umn_WriteMailCmd, "NEWMSGWINDOW TO=\"%a\" SUBJECT=\"%s\" CONTENTS=\"%f\"");
  449.  
  450.     AddTail((struct List *)&up->up_MailerList, (struct Node *)mn);
  451.  
  452.     if(!(mn = AllocMem(sizeof(struct URL_MailerNode), MEMF_CLEAR)))
  453.         return(FALSE);
  454.  
  455.     strcpy(mn->umn_Name, "THOR");
  456.     strcpy(mn->umn_Path, "SYS:Rexxc/RX ");
  457.     GetVar("THOR/THORPath", &mn->umn_Path[13], UMN_PATH_LEN - 13, GVF_GLOBAL_ONLY);
  458.     AddPart(mn->umn_Path, "Rexx/SendThorMail.rexx ADDRESS=\"%a\" SUBJECT=\"%s\" TEXT=\"%f\"", UMN_PATH_LEN);
  459.  
  460.     AddTail((struct List *)&up->up_MailerList, (struct Node *)mn);
  461.  
  462.     if(!(mn = AllocMem(sizeof(struct URL_MailerNode), MEMF_CLEAR)))
  463.         return(FALSE);
  464.  
  465.     strcpy(mn->umn_Name, "YAM 2.0");
  466.     strcpy(mn->umn_Path, "YAM:YAM MAILTO=\"%a\" SUBJECT=\"%s\" LETTER=\"%f\"");
  467.     strcpy(mn->umn_Port, "YAM");
  468.     strcpy(mn->umn_ShowCmd, "SHOW");
  469.     strcpy(mn->umn_ToFrontCmd, "SCREENTOFRONT");
  470.     strcpy(mn->umn_WriteMailCmd, "\"MAILWRITE; WRITETO '%a'; WRITESUBJECT '%s'; WRITELETTER '%f'\"");
  471.  
  472.     AddTail((struct List *)&up->up_MailerList, (struct Node *)mn);
  473. }
  474.  
  475. /**************************************************************************/
  476.  
  477. VOID ConvertPrefsV2toV3(struct URL_Prefs *up)
  478. {
  479.     struct URL_BrowserNode *bn;
  480.  
  481.     for(bn = (struct URL_BrowserNode *)Prefs->up_BrowserList.mlh_Head;
  482.         bn->ubn_Node.mln_Succ;
  483.         bn = (struct URL_BrowserNode *)bn->ubn_Node.mln_Succ)
  484.     {
  485.         STRPTR p;
  486.  
  487.         if(bn->ubn_Flags & UBNF_URLONCMDLINE)
  488.         {
  489.             int len = strlen(bn->ubn_Path);
  490.  
  491.             if(len && (len < (UBN_PATH_LEN - 5)))
  492.                 strcpy(&bn->ubn_Path[len], " \"%u\"");
  493.  
  494.             bn->ubn_Flags &= ~UBNF_URLONCMDLINE;
  495.         }
  496.  
  497.         if((p = strstr(bn->ubn_OpenURLCmd, "%s")))
  498.             *(p + 1) = 'u';
  499.  
  500.         if((p = strstr(bn->ubn_OpenURLWCmd, "%s")))
  501.             *(p + 1) = 'u';
  502.     }
  503. }
  504.